package com.ynet.middleground.riskmanage.model.qichacha;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ynet.core.common.BaseReqObjPager;
import com.ynet.core.common.PagerInfo;
import com.ynet.core.exception.BusinessException;
import com.ynet.middleground.riskmanage.bean.qichacha.operationstate.ADSTLicenseDetailReq;
import com.ynet.middleground.riskmanage.bean.qichacha.operationstate.ADSTLicenseListReq;
import com.ynet.middleground.riskmanage.bean.qichacha.operationstate.TaxCreditReq;
import com.ynet.middleground.riskmanage.dao.QichachaAdministrativeLicenseDetailMapper;
import com.ynet.middleground.riskmanage.dao.QichachaAdministrativeLicenseListMapper;
import com.ynet.middleground.riskmanage.dto.qichacha.operationstate.ADSTLicenseDetailDTO;
import com.ynet.middleground.riskmanage.dto.qichacha.operationstate.ADSTLicenseListDTO;
import com.ynet.middleground.riskmanage.dto.qichacha.operationstate.TaxCreditDTO;
import com.ynet.middleground.riskmanage.dto.qichacha.personcorrelation.PersonHoldingInfoDTO;
import com.ynet.middleground.riskmanage.entity.QichachaAdministrativeLicenseDetail;
import com.ynet.middleground.riskmanage.entity.QichachaAdministrativeLicenseList;
import com.ynet.middleground.riskmanage.entity.QichachaCourtAnnoDetailResp;
import com.ynet.middleground.riskmanage.external.qcc.ApiHelper;
import com.ynet.middleground.riskmanage.external.qcc.QccUriEnum;
import com.ynet.middleground.riskmanage.utils.BeanUtil;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName OperationStateModel
 * @Description 企查查经营状况接口服务处理类
 * @Author zhangyongbin
 * @Date 2020/8/10 15:59
 * @Version 1.0
 */
@Component
public class OperationStateModel {
    @Autowired
    private ApiHelper apiHelper;

    @Autowired
    private Mapper mapper;

    @Autowired
    private QichachaAdministrativeLicenseListMapper qichachaAdministrativeLicenseListMapper;

    @Autowired
    private QichachaAdministrativeLicenseDetailMapper qichachaAdministrativeLicenseDetailMapper;


    /**
     * @Description 行政许可列表（信用中国）
     * @Date 13:59 2020/8/11
     * @Param req  行政许可列表（信用中国）服务请求类
     * @return java.util.List<com.ynet.middleground.riskmanage.dto.qichacha.operationstate.ADSTLicenseListDTO>
     **/
    public List<ADSTLicenseListDTO> getAdministrativeLicenseList(ADSTLicenseListReq req){
        Map<String, Object> params = new HashMap<>(2);
        params.put("searchKey", req.getSearchKey());
        setReqPagingInfo(params, req);
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_ADMINISTRATIVE_LICENSE_LIST, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        setPageInfo(apiResult, req);
        JSONArray result = apiResult.getJSONObject("Result").getJSONArray("Data");
//        Map<String,String> map  = new HashMap(1);
//        //进行key值转换
//        map.put("Id","innerId");
//        result =   BeanUtil.changeJsonArr(result,map);
//        List<QichachaAdministrativeLicenseList> licenseList = null;
//
//        if (result != null) {
//            licenseList = result.toJavaList(QichachaAdministrativeLicenseList.class);
//            for (QichachaAdministrativeLicenseList licence : licenseList) {
//                BeanUtil.setGenericInfo(licence, req);
//                qichachaAdministrativeLicenseListMapper.insert(licence);
//            }
//        }
        System.out.println(apiResult.getJSONObject("Result").get("VerifyResult").toString());
        if("1".equals(apiResult.getJSONObject("Result").get("VerifyResult").toString())){
            List<ADSTLicenseListDTO> aDSTLicenseListDTOList =
                    result.toJavaList(ADSTLicenseListDTO.class);
            return aDSTLicenseListDTOList;
        }else {
            return null;
        }
    }

    /**
     * @Description 行政许可详情（信用中国）
     * @Date 14:00 2020/8/11
     * @Param req -- 行政许可详情（信用中国）服务请求类
     * @return com.ynet.middleground.riskmanage.dto.qichacha.operationstate.ADSTLicenseDetailDTO
     **/
    public ADSTLicenseDetailDTO getAdministrativeLicenseDetail(ADSTLicenseDetailReq req){
        Map<String, Object> params = new HashMap<>(2);
        params.put("id", req.getId());
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_ADMINISTRATIVE_LICENSE_DETAIL, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        JSONObject result = apiResult.getJSONObject("Result");
        Map<String,String> map  = new HashMap(1);
        //进行key值转换
        map.put("Id","innerId");
        result =   BeanUtil.changeJsonObj(result,map);
        QichachaAdministrativeLicenseDetail licenseDetail = null;

        //解析返回结果入库，暂时不做处理
        if (result != null) {
            licenseDetail = result.toJavaObject(QichachaAdministrativeLicenseDetail.class);

                BeanUtil.setGenericInfo(licenseDetail, req);
//            qichachaAdministrativeLicenseDetailMapper.insert(licenseDetail);

        }
        ADSTLicenseDetailDTO aDSTLicenseDetailDTO = result.toJavaObject(ADSTLicenseDetailDTO.class);

        return aDSTLicenseDetailDTO;
    }


    /**
     * @Description  税务信用列表服务处理列
     * @Date 14:26 2020/8/11
     * @Param taxCreditReq -- 税务信用列表请求信息
     * @return java.util.List<com.ynet.middleground.riskmanage.dto.qichacha.operationstate.TaxCreditDTO>
     **/
    public List<TaxCreditDTO> getTaxCreditList(TaxCreditReq req){
        Map<String, Object> params = new HashMap<>(2);
        params.put("searchKey", req.getSearchKey());
        JSONObject apiResult = apiHelper.sendApi(QccUriEnum.GET_TAX_CREDIT_LIST, params, req);
        Integer code = apiResult.getInteger("Status");
        String message = apiResult.getString("Message");
        if (!code.equals(ApiHelper.STATUS_SUCCESS)) {
            throw new BusinessException(message, String.valueOf(code));
        }
        JSONArray result = apiResult.getJSONArray("Result");
        List<TaxCreditDTO> taxCreditList = null;

        if (result != null) {
            taxCreditList = result.toJavaList(TaxCreditDTO.class);
//            for (QichachaAdministrativeLicenseList licence : licenseList) {
//                BeanUtil.setGenericInfo(licence, req);
//                qichachaAdministrativeLicenseListMapper.insert(licence);
//            }
        }
//        List<ADSTLicenseListDTO> aDSTLicenseListDTOList =
//                BeanUtil.mapList(mapper, licenseList, ADSTLicenseListDTO.class);
        return taxCreditList;
    }


    /**
     * 设置企查查分页请求信息参数
     *
     * @param params 请求参数集合
     * @param reqPager 当前分页请求类
     */
    protected void setReqPagingInfo(Map<String, Object> params, BaseReqObjPager reqPager) {
        params.put("pageIndex", reqPager.getPagerInfo().getPageNum());
        params.put("pageSize", reqPager.getPagerInfo().getPageSize());
    }

    /**
     * 如果 api 结果返回分页信息，则将分页结果重新添加到请求分页对象中
     *
     * @param apiResult 企查查 api 结果
     * @param reqPager 请求分页信息
     */
    protected void setPageInfo(JSONObject apiResult, BaseReqObjPager reqPager) {
        JSONObject paging = apiResult.getJSONObject("Paging");
        if (paging == null) {
            return;
        }
        PagerInfo pagerInfo = reqPager.getPagerInfo();
        pagerInfo.setTotal(paging.getInteger("TotalRecords"));
    }

}
